package Leetcode2.链表;

import leetcodeHis.算法总纲.链表.ListNode;

public class Node {
    public static void main(String[] args) {
        ListNode one = new ListNode(0);
        one.next = new ListNode(1);
        one.next.next = new ListNode(2);
        one.next.next.next = new ListNode(3);

        printListNode(one);

        Node node = new Node();
        ListNode last = node.traverseFirN(one, 2);

        printListNode(last);
    }

    private static void printListNode(ListNode one) {
        ListNode head = one;
        while (head!=null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

    //反转整个链表
    private static ListNode traverse(ListNode head) {
        if(head== null || head.next == null) {
            return head;
        }
        ListNode last = traverse(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }

    private ListNode nextIndex = null;

    //反转链表前N个节点
    private ListNode traverseFirN(ListNode head, int n) {
        if(head == null || head.next == null) {
            return head;
        }
        if(n == 1) {
            this.nextIndex = head.next;
            return head;
        }
        ListNode theN = traverseFirN(head.next, n-1);
        head.next.next = head;
        head.next = nextIndex;
        return theN;
    }

    //给定数字k，每k个节点反转
//    private ListNode traversePerK(ListNode head, int k) {
//        //快慢指针
//        ListNode fast = head;
//        ListNode slow = head;
//
//        int step = 0;
//        while (step++ <=k && fast!=null) {
//            fast = fast.next;
//        }
//        if(fast != null) {
//            return head;
//        }
//
//
//
//    }
}
